/**
 * 类名：SchedulePaserHandler
 * 创建日期： 2014-10-22 下午3:41:31
 * 
 */
package com.erms.domain.handler;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.erms.bean.Course;
import com.erms.bean.Department;
import com.erms.bean.Major;
import com.erms.bean.StudentTeach;
import com.erms.bean.StudentTeachId;
import com.erms.bean.Teach;
import com.erms.bean.User;
import com.erms.util.LogUtil;
import com.erms.util.http.XmlParserHandler;

/**
 * @author hmb
 *         备注：
 * 
 */
public class SchedulePaserHandler implements XmlParserHandler<StudentTeach> {

	private Logger	logger	= null;
	private String	id;
	private String	password;

	public SchedulePaserHandler(String id, String password) {

		this.id = id;
		this.password = password;
		logger = LogUtil.getLogger(getClass());
	}

	/**
	 * @see com.erms.util.http.XmlParserHandler#onParse(org.jsoup.nodes.Document)
	 * @param document
	 * @return
	 */
	@Override
	public List<StudentTeach> onParse(Document document) {

		List<StudentTeach> scheduleList = new ArrayList<>();

		// 保存学生信息
		User student = new User(this.id, this.password, "student");


		// ------------------------解析课程信息-------------------------
		Element table = document.select("table").first();
		Elements tr = table.select("tr");// 课表的每一行
		int m = tr.size();
		logger.debug("tr length: " + m);

		// 第一行为星期几的信息，对于本次解析是无用的
		/*
		 * Elements dayElements = tr.first().select("th");
		 * for (int i = 0; i < dayElements.size(); i++) {
		 * logger.debug(dayElements.get(i).text());
		 * }
		 */

		// 设置一个标志标识首次添加课程信息，仅在第一次添加冗余的用户信息
		try {
			
			for (int i = 1; i < m; i++) {
				Elements courseElements = tr.get(i).select("td");
				int n = courseElements.size();
				logger.debug("td length: " + n);
				
				// 第n行表格是无用信息
				for (int j = 0; j < n - 1; j++) {
					// courseInfo的字段格式如下："计算机网络(06020102-1) Ⅱ教学楼II-113 吕建勇 6-13周"
					String courseInfo = courseElements.get(j).text();
					logger.debug("课程信息：" + courseInfo);
					// 不为空(发现由于编码问题，有些空格去不掉，暂且以长度足够长判断是否有课)表示 星期j+1第i大节 有课
					if (courseInfo.length() > 5) {
						// 转化格式如下："计算机网络 06020102-1 Ⅱ教学楼II-113 吕建勇 6-13周"
						courseInfo = courseInfo.trim().replace('(', ' ')
								.replace(")", "");
						logger.debug("转化后的课程信息：" + courseInfo);
						// 考虑到有时候同一大节可能有几个课，仅是上课周数不同
						String[] allCourseInfos = courseInfo.split("周");// 注意没有空格，分出同一大节的不同课
						StudentTeach schedule = null;
						StudentTeachId studentTeachId = null;
						Teach teach = null;
					//	User teacher = null;
						
						//同一大节的课
						for (int courseNum = 0; courseNum < allCourseInfos.length; courseNum++) {
							String[] courseInfos = allCourseInfos[courseNum]
									.trim().split(" ");
							String courseId = "未知";
							String courseSeq = "未知";
							logger.debug("course num " + courseInfos.length);
							courseId = courseInfos[1].split("-")[0];
							courseSeq = courseInfos[1].split("-")[1];
						
							schedule = new StudentTeach();// 课表中的每一节
						
							studentTeachId = new StudentTeachId();
							teach = new Teach();
							teach.setId(courseId + "-" + courseSeq);

							studentTeachId.setTeach(teach);
								studentTeachId.setUser(student);
							schedule.setId(studentTeachId);

							scheduleList.add(schedule);
						}

					} else {
						logger.debug("木有课");
					}
				}

			}

		} catch (Exception e) {			
			logger.debug("发生了未知错误", e);
		}
		LogUtil.divide();
		return scheduleList;
	}

	/**
	 * @see com.erms.util.http.XmlParserHandler#onError()
	 */
	@Override
	public void onError() {

		// TODO 自动生成的方法存根

	}

}
